package com.gzy.android_douban.ui.classify;

import android.content.Intent;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.gzy.android_douban.R;
import com.gzy.android_douban.data.model.ClassifyTime;
import com.gzy.android_douban.data.model.ClassifyType;
import com.gzy.android_douban.databinding.FragmentClassifyBinding;

import java.util.ArrayList;
import java.util.List;


public class ClassifyFragment extends Fragment {
    FragmentClassifyBinding binding;

    public ClassifyFragment() {
    }
    public static ClassifyFragment newInstance() {
        ClassifyFragment fragment = new ClassifyFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentClassifyBinding.inflate(inflater,container,false);

        Resources res =getResources();
        String[] classify_type_item = res.getStringArray(R.array.classify_type_item);
        List<ClassifyType> classify_type_list = new ArrayList<ClassifyType>();
        String[] classify_time_item = res.getStringArray(R.array.classify_time_item);
        List<ClassifyTime> classify_time_list = new ArrayList<ClassifyTime>();

        View.OnClickListener textViewClickListener = new  View.OnClickListener(){

            @Override
            public void onClick(View v) {
//                Toast.makeText(getActivity(), "111 : " + v.getTag(),Toast.LENGTH_SHORT).show();
                Toast.makeText(getActivity(), "222 : " + ((TextView)binding.linearTypeItem.getChildAt((Integer) v.getTag())).getText(),Toast.LENGTH_SHORT).show();
                for (int i = 0;i < classify_type_item.length;i++){
                    if (i == (Integer) v.getTag()){
                        classify_type_list.get((Integer) v.getTag()).setChecked(true);
                        ((TextView) binding.linearTypeItem.getChildAt(i)).getPaint().setFakeBoldText(classify_type_list.get((Integer) v.getTag()).getChecked());

                    }else {
                        classify_type_list.get(i).setChecked(false);
                        ((TextView) binding.linearTypeItem.getChildAt(i)).getPaint().setFakeBoldText(classify_type_list.get((Integer) v.getTag()).getChecked());
                    }
                }
            }
        };

        View.OnClickListener timeViewClickListener = new  View.OnClickListener(){

            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "111 : " + v.getTag(),Toast.LENGTH_SHORT).show();
                for (int i = 0;i < classify_time_item.length;i++){
                    if (i == (Integer) v.getTag()){
                        classify_time_list.get((Integer) v.getTag()).setChecked(true);
                        ((TextView) binding.linearTimeItem.getChildAt(i)).getPaint().setFakeBoldText(classify_time_list.get((Integer) v.getTag()).getChecked());
                    }else {
                        classify_type_list.get(i).setChecked(false);
                        ((TextView) binding.linearTimeItem.getChildAt(i)).getPaint().setFakeBoldText(classify_time_list.get((Integer) v.getTag()).getChecked());
                    }
                }
            }
        };

        for (int i = 0;i < classify_type_item.length;i++){
            ClassifyType classifyType;
            if (i == 0){
                classifyType = new ClassifyType(classify_type_item[i],true);
            }else{
                classifyType = new ClassifyType(classify_type_item[i],false);
            }
            classify_type_list.add(classifyType);
            TextView textView = new TextView(getContext());
            textView.setText(classify_type_list.get(i).getClassTypeName());
            textView.setPadding(7,7,7,7);
            textView.getPaint().setFakeBoldText(classify_type_list.get(i).getChecked());
            textView.setTag(i);
            textView.setClickable(true);
            textView.setOnClickListener(textViewClickListener);
            binding.linearTypeItem.addView(textView);
        }

        for (int i = 0;i < classify_time_item.length;i++){
            ClassifyTime classifyTime;
            if (i == 0){
                classifyTime = new ClassifyTime(classify_time_item[i],true);
            }else{
                classifyTime = new ClassifyTime(classify_time_item[i],false);
            }

            classify_time_list.add(classifyTime);
            TextView timeView = new TextView(getContext());
            timeView.setText(classify_time_list.get(i).getClassTimeName());
            timeView.setPadding(7,7,7,7);
            timeView.getPaint().setFakeBoldText(classify_time_list.get(i).getChecked());
            timeView.setTag(i);
            timeView.setClickable(true);
            timeView.setOnClickListener(timeViewClickListener);
            binding.linearTimeItem.addView(timeView);
        }

        return binding.getRoot();
    }


}

